using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MMXSaveGameConverter
{
    /// <summary>
    /// Логика взаимодействия для SaveGameControl.xaml
    /// </summary>
    public partial class SaveGameControl : UserControl
    {
        public SaveGameControl()
        {
            InitializeComponent();
            SaveGameFormatComboBox.Items.Add(SaveGameFormat.Xml);
            SaveGameFormatComboBox.Items.Add(SaveGameFormat.Save);
            SaveGameFormatComboBox.Items.Add(SaveGameFormat.Sav);
            SaveGameFormatComboBox.Items.Add(SaveGameFormat.Lgs);
            SaveGameFormatComboBox.SelectedIndex = 0;
        }

        private void OnConvertClick(object sender, RoutedEventArgs e)
        {
            try
            {
                SaveGameViewItem item = DataContext as SaveGameViewItem;
                if (item == null)
                {
                    MessageBox.Show("Please select save game first.");
                    return;
                }

                SaveGameFormat format = (SaveGameFormat)SaveGameFormatComboBox.SelectedValue;
                if (item.Format == format)
                {
                    MessageBox.Show("Please select other output format first.");
                    return;
                }

                Convert(item, format);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Convert(SaveGameViewItem item, SaveGameFormat format)
        {
            switch (format)
            {
                case SaveGameFormat.Xml:
                    ToXml(item);
                    break;
                case SaveGameFormat.Save:
                    ToSave(item);
                    break;
                case SaveGameFormat.Lgs:
                    ToLgs(item);
                    break;
                case SaveGameFormat.Sav:
                    ToSav(item);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("value");
            }
        }

        private void ToXml(SaveGameViewItem item)
        {
            XmlFileWriter writer = new XmlFileWriter(item);
            writer.Write();
        }

        private void ToSave(SaveGameViewItem item)
        {
            SaveFileWriter writer = new SaveFileWriter(item);
            writer.Write();
        }

        private void ToLgs(SaveGameViewItem item)
        {
            LgsFileWriter writer = new LgsFileWriter(item);
            writer.Write();
        }

        private void ToSav(SaveGameViewItem item)
        {
            SavFileWriter writer = new SavFileWriter(item);
            writer.Write();
        }
    }
}